﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using LitJson;

[AddComponentMenu("PUEX/Affector/Size/ScaleOverLifetime")]
public class PUEXScaleOverLifetimeAffector : PUEXAffector {

	#region - Editable property
	public PUEXDynamicAttribute scaleXOverLifetime;
	public PUEXDynamicAttribute scaleYOverLifetime;
	public bool useEmitterTime;
	#endregion
	
	#region - Lifecycle
	public PUEXScaleOverLifetimeAffector ()
	{
		scaleXOverLifetime = PUEXDynamicAttribute.GenerateFixed (1f);
		scaleYOverLifetime = PUEXDynamicAttribute.GenerateFixed (1f);
		useEmitterTime = false;
	}
	#endregion
	
	#region - Update
	public override void update (float dt, PUEXParticle particle)
	{
		float t = particle.timeFraction;
		if (useEmitterTime)
			t = inTrigger () ? triggerTimeFraction : ownerEmitter.timeFraction;

		particle.scale = Vector2.Scale (particle.originalScale, new Vector2 (scaleXOverLifetime.getValue(t), scaleYOverLifetime.getValue(t)));
	}
	#endregion

	#region - Param property
	public override void updateParams (PUEXParticleSystem system)
	{
		scaleXOverLifetime.parameterize (system);
		scaleYOverLifetime.parameterize (system);
	}
	#endregion

	#region - Serialization
	public override JsonData save (JsonData emitterData, JsonData systemData)
	{
		JsonData ret = base.save (emitterData, systemData);
		
		ret ["scale_x_over_lifetime"] = PUEXUtils.serializeDynamicAttribute (scaleXOverLifetime);
		ret ["scale_y_over_lifetime"] = PUEXUtils.serializeDynamicAttribute (scaleYOverLifetime);
		ret ["use_emitter_time"] = useEmitterTime;
		
		return ret;
	}
	#endregion

}
